package com.example.zhinongbao.view;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zhinongbao.Model.UserManager;
import com.example.zhinongbao.R;
import com.example.zhinongbao.base.BaseFragment;
import com.example.zhinongbao.contract.MineContract;
import com.example.zhinongbao.entity.User;
import com.example.zhinongbao.entity.UserInfo;
import com.example.zhinongbao.fragment.LoginActivity;
import com.example.zhinongbao.presenter.MinePresenter;
import com.example.zhinongbao.utils.SpUtils;

public class MineFragment extends BaseFragment implements View.OnClickListener, MineContract.IMineView {

    private TextView mTvName, mallorders, mTvid;
    private LinearLayout mSettingLayout, toBeShipped, toBeEvaluated, afterSales, toBeReceived, personal_center,shopping_cart;
    private MineContract.IMinePresenter mMinePresenter;
    private SpUtils mSpUtils;
    private ImageView mImageView;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView() {
        mMinePresenter = new MinePresenter(this, getContext());
        mSettingLayout = mView.findViewById(R.id.mine_setting_btn);
        mTvName = mView.findViewById(R.id.mine_user_name);
        mTvid = mView.findViewById(R.id.mine_user_id);
        mSpUtils = new SpUtils(mContext);
        toBeShipped = mView.findViewById(R.id.to_be_shipped);
        toBeReceived = mView.findViewById(R.id.to_be_received);
        toBeEvaluated = mView.findViewById(R.id.to_be_evaluated);
        afterSales = mView.findViewById(R.id.after_sales);
        mallorders = mView.findViewById(R.id.allOrders);
        shopping_cart=mView.findViewById(R.id.shopping_cart);
        personal_center = mView.findViewById(R.id.personal_center);

        mImageView = mView.findViewById(R.id.mine_user_avatar);

        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
        String userName = sharedPreferences.getString("username", "");
        String password = sharedPreferences.getString("password", "");
        if (userName.isEmpty() && password.isEmpty()) {
            UserManager.setLoggedIn(false);
        }

//        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
//        String phone = sharedPreferences.getString("phone" , "");
//        mTvid.setText(phone);
//        mTvName.setText(phone);
//        double account = MinePresenter.showNum(phone);
//        String loginAccount = String.valueOf(account);
//        MineBalanceNumber.setText(loginAccount);
    }

    @Override
    public void onResume() {
        super.onResume();
        mMinePresenter.getLoginUser();
    }

    @Override
    protected void initListener() {
        mSettingLayout.setOnClickListener(this);
        toBeShipped.setOnClickListener(this);
        toBeEvaluated.setOnClickListener(this);
        toBeReceived.setOnClickListener(this);
        afterSales.setOnClickListener(this);
        mallorders.setOnClickListener(this);
        shopping_cart.setOnClickListener(this);

        personal_center.setOnClickListener(this);

        /*mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        });*/
        mTvName.setOnClickListener(v -> {
            if (!UserManager.isLoggedIn()) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.mine_setting_btn) {
            Intent intent = new Intent(getActivity(), Setting.class);
            startActivity(intent);
        } else if (id == R.id.allOrders) {
            Intent intent = new Intent(getActivity(), OrdersActivity.class);
            intent.putExtra("case", 1);
            startActivity(intent);
        } else if (id == R.id.to_be_shipped) {
            Intent intent = new Intent(getActivity(), OrdersActivity.class);
            intent.putExtra("case", 2);
            startActivity(intent);
        } else if (id == R.id.to_be_received) {
            Intent intent = new Intent(getActivity(), OrdersActivity.class);
            intent.putExtra("case", 3);
            startActivity(intent);
        } else if (id == R.id.to_be_evaluated) {
            Intent intent = new Intent(getActivity(), OrdersActivity.class);
            intent.putExtra("case", 4);
            startActivity(intent);
        } else if (id == R.id.after_sales) {
            Intent intent = new Intent(getActivity(), OrdersActivity.class);
            intent.putExtra("case", 5);
            startActivity(intent);
        } else if (id == R.id.personal_center) {
            Intent intent = new Intent(getActivity(), PersonalCenter.class);
            startActivity(intent);
        }else if (id == R.id.shopping_cart) {
            Intent intent = new Intent(getActivity(), ShoppingCartActivity.class);
            startActivity(intent);
        }

    }

    //   @Override
//   public void showUserInfo(UserInfo userInfo) {
//      if (mTvName != null && userInfo != null) {
//         mTvName.setText(userInfo.getUsername());
//          mTvid.setText(userInfo.getPhone());
//
//      }
//    }
    @Override
    public void showUserInfo(UserInfo userInfo) {
        // 检查 userInfo 是否为 null，如果为 null 表示用户未登录
        if (userInfo == null) {
            // 用户未登录，设置界面元素显示为空白或隐藏
            if (mTvName != null) {
                mTvName.setText("点击登录"); // 或者使用 mTvName.setVisibility(View.GONE); 来隐藏
            }
            if (mTvid != null) {
                mTvid.setText("");
//            mTvid.setText("点击登录"); // 或者使用 mTvid.setVisibility(View.GONE); 来隐藏
            }
        } else {
            // 用户已登录，显示用户名和电话号码
            if (mTvName != null && mTvid != null) {
                mTvName.setText(userInfo.getUsername());
                mTvid.setText(userInfo.getPhone());
            }
        }
    }
}
